<?php
namespace app\oem\controller;
use think\Controller;
use think\Db;
use app\common\controller\OemBaseController;

class Count extends OemBaseController
{
	//统计
     public function summary()
    {
    	$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
			$map['oem_id']=session('d_id');
		}elseif($user_level==2){//省代理
			$where['agency_id1']=session('d_id');
			$map['agency_id']=session('d_id');
		}elseif($user_level==3){
			$where['agency_id2']=session('d_id');
			$map['city_id']=session('d_id');
		}	
		
		$oemlevel2=db('info_oem')->where($map)->where('user_level',2)->count();
		$oemlevel3=db('info_oem')->where($map)->where('user_level',3)->count();
		
		$mems=db('info_user')->where($where)->count();
		$agents=db('info_agent')->where($where)->count();

		$list=db("log_paylog_copy")->where($where)->field("uid,balance,order_type,score,sy_type,payment_type")->select();
		$data["oemlevel2"]=$oemlevel2;//城市运营中心
		$data["oemlevel3"]=$oemlevel3;//区域服务商
		$data["mems"]=$mems;
		$data["agents"]=$agents;
		$data['mem_orders']=0;//会员消费订单笔数
		$data['mem_orders_ms']=0;//会员消费总额
		$data['sk_orders']=0;//散客消费订单笔数
		$data['sk_orders_ms']=0;//散客消费金额
		$data['sm_orders']=0;//扫码支付订单笔数
		$data['sm_orders_ms']=0;//扫码支付总额
		$data['glj_ms']=0;//鼓励金总额
		$data['glj_zs']=0;//鼓励金赠送
		$data['glj_dk']=0;//鼓励金抵扣
		$data['sc_orders']=0;//商城订单
		$data['sc_orders_ms']=0;//商城订单金额
		
		$data['glj_zs_num']=0;//鼓励金赠送笔数
		$data['glj_dk_num']=0;//鼓励金抵扣笔数	
		$data['glj_cz']=0;//鼓励金充值笔数
		$data['glj_cz_ms']=0;//鼓励金充值
		if($list){
			foreach($list as $k=>$v){
				if($v['payment_type']==1){
					$data['glj_zs_num']++;
					$data['glj_zs']+=$v['score'];
				}elseif($v['payment_type']==2){
					$data['glj_dk_num']++;
					$data['glj_dk']+=$v['score'];
				}
				$data['glj_ms']+=$v['score'];
				if($v['uid']){
					$data['mem_orders']++;
					$data['mem_orders_ms']+=$v['balance'];
				}else{
					$data['sk_orders']++;
					$data['sk_orders_ms']+=$v['balance'];
				}
				 if($v['order_type']==1){
				 	$data['sc_orders']++;
					$data['sc_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==2){
				 	$data['sm_orders']++;
					$data['sm_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==6&&$v['uid']){
				 	$data['sm_orders']++;
					$data['sm_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==8&&$v['uid']){
				 	$data['glj_cz']++;
					$data['glj_cz_ms']+=$v['balance'];
				 }
			}
		}
		$mp=array('jz'=>$data['sk_orders_ms'],"sm"=>$data['sm_orders_ms'],'sc'=>$data['sc_orders_ms']);
		$this->assign('mp',json_encode($mp));
		$this->assign("data",$data);
    	return $this->fetch();
    }

	//城市运营中心
	public function agency_record(){
		$user_level=session("user_level");
		if($user_level==1){//oem
			$map['oem_id']=session('d_id');
			$where['a.oem_id']=session('d_id');
			$where['a.user_level']=2;
		}elseif($user_level==2){//省代理
			$map['agency_id1']=session('d_id');
			$where['a.agency_id']=session('d_id');
		}elseif($user_level==3){
			$map['agency_id2']=session('d_id');
			$where['a.city_id']=session('d_id');
		}
		
		$citynum = db('info_oem')->field('agency_id,count(id) as citynum')->where('user_level',3)->group('agency_id')->buildSql();//服务商数
		$agentnum = db('info_agent')->field('agency_id1,count(id) as agentnum')->where($map)->group('agency_id1')->buildSql();//商户数
		$usernum = db('info_user')->field('agency_id1,count(id) as usernum')->where($map)->group('agency_id1')->buildSql();//会员数
		
		$cznum = db("log_paylog_copy")->field("agency_id1,sum(balance) as cznum")->where($map)->where('order_type',8)->group('agency_id1')->buildSql();
		$zsnum = db("log_paylog_copy")->field("agency_id1,sum(score) as zsnum")->where($map)->where('payment_type',1)->group('agency_id1')->buildSql();
		$dknum = db("log_paylog_copy")->field("agency_id1,sum(score) as dknum")->where($map)->where('payment_type',2)->group('agency_id1')->buildSql();
//		$map['order_type']=['in',[1,2]];
		$znum = db("log_paylog_copy")->field("agency_id1,sum(balance) as znum,count(id) as num")->where($map)->group('agency_id1')->buildSql();
						
    	$list = db('info_oem')->alias('a')
			->join($citynum.'b','a.id = b.agency_id','LEFT')
			->join($agentnum.'c','a.id = c.agency_id1','LEFT')
			->join($usernum.'d','a.id = d.agency_id1','LEFT')
			->join($cznum.'e','a.id = e.agency_id1','LEFT')
			->join($zsnum.'f','a.id = f.agency_id1','LEFT')
			->join($dknum.'g','a.id = g.agency_id1','LEFT')
			->join($znum.'h','a.id = h.agency_id1','LEFT')
		    ->field('a.id,a.add_time,a.user_name,b.citynum,c.agentnum,d.usernum,e.cznum,f.zsnum,g.dknum,h.znum,h.num')
		    ->where($where)
			->order('a.add_time desc')
		    ->select();
        $this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();		
	}
	
	//区域服务商
	public function city_record(){
		$user_level=session("user_level");
		if($user_level==1){//oem
			$map['oem_id']=session('d_id');
			$where['a.oem_id']=session('d_id');
			$where['a.user_level']=3;
		}elseif($user_level==2){//省代理
			$map['agency_id1']=session('d_id');
			$where['a.agency_id']=session('d_id');
		}elseif($user_level==3){
			$map['agency_id2']=session('d_id');
			$where['a.city_id']=session('d_id');
		}
		
		$agentnum = db('info_agent')->field('agency_id2,count(id) as agentnum')->where($map)->group('agency_id2')->buildSql();//商户数
		$usernum = db('info_user')->field('agency_id2,count(id) as usernum')->where($map)->group('agency_id2')->buildSql();//会员数

		$cznum = db("log_paylog_copy")->field("agency_id2,sum(balance) as cznum")->where($map)->where('order_type',8)->group('agency_id2')->buildSql();
		$zsnum = db("log_paylog_copy")->field("agency_id2,sum(score) as zsnum")->where($map)->where('payment_type',1)->group('agency_id2')->buildSql();
		$dknum = db("log_paylog_copy")->field("agency_id2,sum(score) as dknum")->where($map)->where('payment_type',2)->group('agency_id2')->buildSql();
//		$map['order_type']=['in',[1,2]];
		$znum = db("log_paylog_copy")->field("agency_id2,sum(balance) as znum,count(id) as num")->where($map)->group('agency_id2')->buildSql();
						
    	$list = db('info_oem')->alias('a')
			->join($agentnum.'c','a.id = c.agency_id2','LEFT')
			->join($usernum.'d','a.id = d.agency_id2','LEFT')
			->join($cznum.'e','a.id = e.agency_id2','LEFT')
			->join($zsnum.'f','a.id = f.agency_id2','LEFT')
			->join($dknum.'g','a.id = g.agency_id2','LEFT')
			->join($znum.'h','a.id = h.agency_id2','LEFT')
		    ->field('a.id,a.add_time,a.user_name,c.agentnum,d.usernum,e.cznum,f.zsnum,g.dknum,h.znum,h.num')
		    ->where($where)
			->order('a.add_time desc')
		    ->select();
        $this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();		
	}

	//商家
	public function agent_record(){
		$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
			$map['oem_id']=session('d_id');
		}elseif($user_level==2){//省代理
			$where['agency_id1']=session('d_id');
			$map['agency_id1']=session('d_id');
		}elseif($user_level==3){
			$where['agency_id2']=session('d_id');
			$map['agency_id2']=session('d_id');
		}	

		$zsnum=db("log_paylog_copy")->field("agent_id,sum(score) as zsnum")->where($map)->where('payment_type',1)->group('agent_id')->buildSql();
		$dknum=db("log_paylog_copy")->field("agent_id,sum(score) as dknum")->where($map)->where('payment_type',2)->group('agent_id')->buildSql();
		
    	$list = db('info_agent')->alias('a')
			->join($zsnum.'b','a.id = b.agent_id','LEFT')
			->join($dknum.'c','a.id = c.agent_id','LEFT')
		    ->field('a.id,a.agent_name,b.zsnum,c.dknum')
		    ->where($where)
			->order('a.add_time desc')
		    ->paginate(20);
        $this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
	}

	//会员
	public function user_record(){
		$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
			$map['oem_id']=session('d_id');
		}elseif($user_level==2){//省代理
			$where['agency_id1']=session('d_id');
			$map['agency_id1']=session('d_id');
		}elseif($user_level==3){
			$where['agency_id2']=session('d_id');
			$map['agency_id2']=session('d_id');
		}	
		
		$zsnum=db("log_paylog_copy")->field("uid,sum(score) as zsnum")->where($map)->where('payment_type',1)->group('uid')->buildSql();
		$dknum=db("log_paylog_copy")->field("uid,sum(score) as dknum")->where($map)->where('payment_type',2)->group('uid')->buildSql();
		
    	$list = db('info_user')->alias('a')
			->join($zsnum.'b','a.id = b.uid','LEFT')
			->join($dknum.'c','a.id = c.uid','LEFT')
		    ->field('a.id,a.nickname,b.zsnum,c.dknum')
		    ->where($where)
			->order('a.add_time desc')
		    ->paginate(20);
        $this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
	}

   	//明细查询
    public function commission()
    {
	   	if(request()->isPost()){
    		
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}

		$where['a.oem_id'] = session('d_id');		
		$where['a.balance'] = ['<>',0];
//		$where['a.o_id'] = session('d_id');
		//$time="DATE_FORMAT(add_time,'%Y-%m-%d')";
    	$list=Db::table('log_oem_commission')->alias('a')
		    ->field('a.id,a.add_time,a.balance,a.order_type,a.message')
		    ->where($where)
			->order('a.add_time desc')
		    ->paginate(20);
        $this->assign('list',$list);
		
		$sumbalance = db('log_oem_commission')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sumbalance',$sumbalance);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    
    //积分明细
    function score(){
    	
    	if(request()->isPost()){
    		
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}

		$where['a.oem_id'] = session('d_id');		
		$where['a.balance'] = ['<>',0];
    	$list=Db::table('log_oem_score')->alias('a')
		    ->field('a.id,a.add_time,a.balance,a.order_type,a.message')
		    ->where($where)
			->order('a.add_time desc')
		    ->paginate(20);
        $this->assign('list',$list);
		
		$sumbalance = db('log_oem_score')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sumbalance',$sumbalance);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    	
    }
    
    //鼓励金导出
    public function detaiglj_dc(){
		if(!empty(input('begin_time')) && !empty(input('end_time'))){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		
		$where['a.oem_id'] = session('d_id');

		$where['a.balance'] = ['<>',0];
    	$list=Db::table('log_oem_score')->alias('a')
		    ->field('a.id,a.add_time,a.balance,a.order_type,a.message')
		    ->where($where)
			->order('a.add_time desc')
		    ->select();
		
		$str ="类型,详情,变化金额,时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_type = iconv('utf-8','gbk',$row['order_type']);
			$typename = '';
			switch ($order_type) {
				case '1':
					$typename = '订单佣金';
					break;
				case '2':
					$typename = '扫码佣金';
					break;
				case '3':
					$typename = '广告佣金';
					break;
				case '4':
					$typename = '退款佣金';
					break;
				case '5':
					$typename = '佣金转余额';
					break;
				case '6':
					$typename = '线下收银';
					break;
				case '7':
					$typename = '费率佣金';
					break;
				case '8':
					$typename = '会员转账';
					break;			
				case '9':
					$typename = '会员充值鼓励金';
					break;			
				default:
					$typename = '扫码佣金';
					break;
			}
			$typename = iconv('utf-8','gbk',$typename); 		
        	$balance = iconv('utf-8','gbk',$row['balance'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$message = iconv('utf-8','gbk',$row['message'].'	');
			$str .= $typename.",".$message.",".$balance.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='鼓励金报表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
    
    
    public function detailed()
    {
    	if(request()->isPost()){
    		
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
			if(!empty($param['storelist']))$where['a.chain_id'] = $param['storelist'];
			if(!empty($param['ywylist']))$where['a.salesman_id'] = $param['ywylist'];
			if(!empty($param['dplist']))$where['a.agent_id'] = $param['dplist'];
			if(!empty($param['pay_type']))$where['a.pay_type'] = $param['pay_type'];
			if(!empty($param['order_sn']))$where['a.order_sn'] = ['like','%'.$param['order_sn'].'%'];
			
    	}

		$where['a.oem_id'] = session('oem_id');
    	$paylog = db('log_paylog')
    	->alias('a')
		->join('agent_store w','a.chain_id = w.id','LEFT')
		->field('a.*,w.store_name')
    	->where($where)
    	->paginate(8);
		$this->assign('paylog',$paylog);
		
		
		//获取省代
		$info_oem = db('info_oem')->where('user_level',2)->where('status',1)->select();
		$this->assign('info_oem',$info_oem);
		
		//获取总消费额
		$sumbalance = db('log_paylog')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sumbalance',$sumbalance);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }

    public function sdlist()
    {
    	//获取市代
		$data = db('info_oem')->where('agency_id',input('id'))->where('user_level',3)->where('status',1)->select();
		return json(["data"=>$data]);
    } 

    public function ywylist()
    {
		//获取店铺
		$data = db('info_agent')->where('agency_id2',input('id'))->select();
		return json(["data"=>$data]);
    } 

    public function storelist()
    {
    	//获取店铺所属门店
		$data = db('agent_store')->where('agent_id',input('id'))->select();
		return json(["data"=>$data]);
    } 
    
    
    //明细导出
    public function detail_dc(){
		$param=input();
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}
		if(!empty($param['storelist']))$where['a.chain_id'] = $param['storelist'];
		if(!empty($param['ywylist']))$where['a.salesman_id'] = $param['ywylist'];
		if(!empty($param['dplist']))$where['a.tid'] = $param['dplist'];
		if(!empty($param['pay_type']))$where['a.pay_type'] = $param['pay_type'];
		if(!empty($param['order_sn']))$where['a.order_sn'] = ['like','%'.$param['order_sn'].'%'];
				
		$where['a.oem_id'] = session('oem_id');
    	$list = db('log_paylog_copy')
    	->alias('a')
		->join('agent_store w','a.chain_id = w.id','LEFT')
		->field('a.*,w.store_name')
    	->where($where)
    	->select();
		

		$str ="订单号,日期,门店,项目,支付方式,消费金额,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_sn = iconv('utf-8','gbk',$row['order_sn'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$store_name = iconv('utf-8','gbk',$row['store_name']); 
			if($row['sy_type']==1){
				$syname = '记账';
			}elseif($row['sy_type']==2) {
				$syname = '扫码';
			}else{
				$syname = '商品';
			}
			$syname = iconv('utf-8','gbk',$syname); 
			
			if($row['pay_type']==1){
				$payname = '余额';
			}elseif($row['pay_type']==2) {
				$payname = '微信';
			}elseif($row['pay_type']==3) {
				$payname = '支付宝';
			}else{
				$payname = '银联';
			}
			$pay_type = iconv('utf-8','gbk',$payname); 
			$balance = iconv('utf-8','gbk','	'.$row['balance']);
			$str .= $order_sn.",".$add_time.",".$store_name.",".$syname.",".$pay_type.",".$balance.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='明细列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}


	//佣金报表导出
    public function details_dc(){
		if(!empty(input('begin_time')) && !empty(input('end_time'))){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		
		$where['a.oem_id'] = session('d_id');

    	$list=Db::table('log_oem_commission')->alias('a')
		    ->field('a.id,a.add_time,a.balance,a.order_type,a.message')
		    ->where($where)
			->order('a.add_time desc')
		    ->select();
		
		$str ="类型,详情,变化金额,时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_type = iconv('utf-8','gbk',$row['order_type']);
			$typename = '';
			switch ($order_type) {
				case '1':
					$typename = '订单佣金';
					break;
				case '2':
					$typename = '扫码佣金';
					break;
				case '3':
					$typename = '广告佣金';
					break;
				case '4':
					$typename = '退款佣金';
					break;
				case '5':
					$typename = '佣金转余额';
					break;
				case '6':
					$typename = '线下收银';
					break;
				case '7':
					$typename = '费率佣金';
					break;
				case '8':
					$typename = '会员转账';
					break;			
				case '9':
					$typename = '会员充值鼓励金';
					break;			
				default:
					$typename = '扫码佣金';
					break;
			}
			$typename = iconv('utf-8','gbk',$typename); 		
        	$balance = iconv('utf-8','gbk',$row['balance'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$message = iconv('utf-8','gbk',$row['message'].'	');
			$str .= $typename.",".$message.",".$balance.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='佣金报表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	

	//市代理明细查询
 	public function citydetailed()
    {		
		if(input('begin_time') && input('end_time')){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		if(input('dplist'))$where['a.agent_id'] = ['like','%'.input('dplist').'%'];
		if(input('store_name'))$where['a.chain_id'] = ['like','%'.input('store_name').'%'];
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

    	$where['a.agency_id2'] = session('d_id');	
		$where['a.order_type'] = ['not in','3,4,5'];	
		$where['a.sy_type'] = 2;		
    	$paylog = db('log_paylog_copy')
    	->alias('a')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->join('info_agent b','a.agent_id = b.id','LEFT')
		->join('info_user c','a.uid = c.id','LEFT')
		->field('a.*,w.store_name,b.agent_name,c.mobile')
    	->where($where)
		->order('a.add_time desc')
    	->paginate(10,false,['query'=>request()->param()]);
		$this->assign('paylog',$paylog);
		
		//获取店铺
		$info_agent = db('info_agent')->where('agency_id2',session('d_id'))->where('status',1)->select();
		$this->assign('info_agent',$info_agent);
		
		//获取业务员
		$sale_list = db('info_salesman')->where('agency_id2',session('d_id'))->select();
		$this->assign('sale_list',$sale_list);
		
		//获取总消费额
		$sumbalance = db('log_paylog_copy')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sumbalance',$sumbalance);
		
		//获取总分佣
		$commission = db('log_paylog_copy')->alias('a')->where($where)->sum('a.agency_id2_commission');
		$this->assign('commission',$commission);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }

	//根据业务员获取商家
    public function salemanlist()
    {
    	//获取店铺所属门店
		$data = db('info_agent')->where('salesman_id',input('id'))->select();
		return json(["data"=>$data]);
    } 
	
	//明细导出
    public function citydetail_dc(){
		if(input('begin_time') && input('end_time')){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		if(input('dplist'))$where['a.agent_id'] = ['like','%'.input('dplist').'%'];
		if(input('store_name'))$where['a.chain_id'] = ['like','%'.input('store_name').'%'];
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

    	$where['a.agency_id2'] = session('d_id');	
		$where['a.order_type'] = ['not in','3,4,5'];	
		$where['a.sy_type'] = 2;		
    	$paylog = db('log_paylog_copy')
    	->alias('a')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->join('info_agent b','a.agent_id = b.id','LEFT')
		->join('info_user c','a.uid = c.id','LEFT')
		->field('a.*,w.store_name,b.agent_name,c.mobile')
    	->where($where)
		->order('a.add_time desc')
    	->select();

		$str ="订单号,店铺,门店,会员,支付方式,消费金额,分佣,日期,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($paylog as $k=>$row){
        	$order_sn = iconv('utf-8','gbk',$row['order_sn'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$agent_name = iconv('utf-8','gbk',$row['agent_name']); 
			$store_name = iconv('utf-8','gbk',$row['store_name']);
			$mobile = iconv('utf-8','gbk',$row['mobile']?$row['mobile'].'	':'扫码会员');
			$oem_mission = iconv('utf-8','gbk',$row['oem_commission']); 		
			if($row['pay_type']==1){
				$payname = '余额';
			}elseif($row['pay_type']==2) {
				$payname = '微信';
			}elseif($row['pay_type']==3) {
				$payname = '支付宝';
			}else{
				$payname = '银联';
			}
			$pay_type = iconv('utf-8','gbk',$payname); 
			$balance = iconv('utf-8','gbk','	'.$row['balance']);
			$str .= $order_sn.",".$agent_name.",".$store_name.",".$mobile.",".$pay_type.",".$balance.",".$oem_mission.",".$add_time.","."\n"; //用引文逗号分开  
        } 

	  	$filename ='明细列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}

	//商家列表年审
    public function agent_limited()
    {
    	
		if(input('mobile') && input('mobile')>0){
			$where['a.mobile'] = ['like','%'.input('mobile').'%'];
		}
		
		if(input('agent_name') && input('agent_name')>0){
			$where['a.agent_name'] = ['like','%'.input('agent_name').'%'];
		}
		
		if(input('is_state') && input('is_state')>0){
			$where['a.is_state'] = input('is_state');
		}
		
		$shagent = Db::table('info_agent')->field("datediff(effective_time,now()) as jgtime,id")
		->where('datediff(effective_time,now())<=10')->buildSql();
    	
    	$where['a.id'] = ['>',0];
		$where['a.status'] = 1;	
    	$list=Db::table('info_agent a')->
		join("info_oem b","a.agency_id2=b.id",'LEFT')->
		join("info_oem c","a.agency_id1=c.id",'LEFT')->
		join($shagent." d","a.id=d.id",'inner')		
    	->field("a.*,d.jgtime,b.user_name as pname,c.user_name as cname")
    	->order('add_time desc')
		->where($where)
    	->paginate(20,false,['query'=>request()->param()]);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	$this->assign('list',$list);
    	return $this->fetch();
    }

	//审核通过
	public function limited_pass(){
		$id=input('id');
		$where['effective_time']=date('Y-m-d H:i:s',strtotime('+1year'));
		$results=Db::name('info_agent')->where('id',$id)->update($where);

        if($results){
			return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
    	return $this->fetch();
    }
	
	//年审不通过冻结
	public function complain_frozen(){
		$data['status'] = 2;
	    $result=Db::table('info_agent')->where('id',$_GET['id'])->update($data);
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       	}else{
       		return json(['code'=>402,'msg'=>'失败']);
       	}
		
		$data=Db::table('info_oem')->where('user_type=1 and user_level=3')->select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    
    //商家银行资质审核
    public function agent_bankinfo()
    {
		if(input('mobile') && input('mobile')>0){
			$where['a.mobile'] = ['like','%'.input('mobile').'%'];
		}
		
		if(input('agent_name')){
			$where['b.agent_name1'] = ['like','%'.input('agent_name').'%'];
		}
		
		if(input('state') && input('state')>0){
			$where['b.state'] = input('state');
		}else{
			$where['b.state'] = ['<>',1];
		}
		
		$where['a.status'] = 1;				
		$list=Db::table('info_agent')
    	->alias('a')
    	->join('agent_qianyue b','a.id = b.agent_id')
    	->field('b.agent_name1 as agent_name,b.contacts as contacts,b.state as state,b.add_time as add_time,a.id as id,b.id as sid,a.mobile')
		->order('b.add_time desc')
		->where($where)
		->paginate(20,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//审核通过
	public function examine_pass(){
		$id=input('id');
		//商家资质审批通过 4 初审通过
//		$results=Db::name('info_agent')->where('id',$id)->update(["is_state"=>1]);
		$results = db("agent_qianyue")->where("agent_id",$id)->update(["state"=>4]);

        if($results){
    		//获取商家信息
    		$agent_info = Db::name('info_agent')->where('id',$id)->find();
			//短信开关 1 开  0 关
			if(config('sms_flag')==1){
	        	//发送短信提醒财务
	        	$tem_w['title']='审核成功';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=sendsms($tem['sign_name'],config('send_cwmobile'),$tem['tid'],'code',$agent_info['mobile']."签约资料已通过初审",config('cut_oem_id'));
			}
			return json(['code'=>200,'msg'=>'审核成功']);
       }else{
       		return json(['code'=>402,'msg'=>'审核失败']);
       }
    	
    	return $this->fetch();
    }
	
	//审核驳回
	public function agent_reject(){
		$this->assign('id',input('id'));
    	return $this->fetch();
    }
	
	//审核驳回
	public function examine_reject(){
		$id=input('id');
		//驳回 银行资质
		$where['is_state']=3;
		$result=Db::name('info_agent')->where('id',$id)->update($where);
		$update['state']=3;
		$update['reason']=input('content');
		$results=db("agent_qianyue")->where("agent_id",$id)->update($update);
        if($results){
        	//短信开关 1 开  0 关
			if(config('sms_flag')==1){
	    		//获取商家信息
	    		$agent_info = Db::name('info_agent')->where('id',$id)->find();
	    		//获取商家服务商信息
	    		$city_info = Db::name('info_oem')->where('id',$agent_info['agency_id2'])->find();
				
				//发送短信提醒商家
	        	$tem_w['title']='审核失败';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=sendsms($tem['sign_name'],$agent_info['mobile'],$tem['tid'],'code','商家',config('cut_oem_id'));
				    	
	        	//发送短信提醒服务商
		        $tem_w['title']='审核失败';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=sendsms($tem['sign_name'],$city_info['mobile'],$tem['tid'],'code',$city_info['user_name']."：".$agent_info['mobile']."签约资料未审核通过",config('cut_oem_id'));			
			}
			return json(['code'=>200,'msg'=>'操作成功']);
       	}else{
       		return json(['code'=>402,'msg'=>'操作失败']);
       	}
    	return $this->fetch();
    }
}
